package com.zhika.zishi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhika.zishi.R;
import java.util.ArrayList;

/**
 * Created by Michael on 2015/4/6.
 */
public class SelectHobbyAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<String> hobbyList;

    private boolean[] b;

    public SelectHobbyAdapter(Context context, ArrayList<String> hobbyList) {
        this.context = context;
        this.hobbyList = hobbyList;
        b = new boolean[getCount()];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return hobbyList.get(position);
    }

    @Override
    public int getCount() {
        return hobbyList.size();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;
        final TextView textView;
        if (convertView == null) {

            view = LayoutInflater.from(context).inflate(R.layout.item_of_select_hobby_gird, null);
            textView = (TextView) view.findViewById(R.id.item_of_select_hobby_text);

            view.setTag(textView);
        } else {
            view = convertView;
            textView = (TextView) view.getTag();
            textView.setText("");
        }

        textView.setText((String) getItem(position));

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!b[position]) {
                    textView.setBackground(context.getResources().getDrawable(R.drawable.text_2_shape));
                    textView.setTextColor(context.getResources().getColor(R.color.color_white));
                    b[position] = true;
                } else {
                    textView.setBackground(context.getResources().getDrawable(R.drawable.text_1_shape));
                    textView.setTextColor(context.getResources().getColor(R.color.color_yellow));
                    b[position] = false;
                }
            }
        });
        return view;
    }
}
